package models;

import java.io.File;
import java.io.IOException;

import objects.Feature;
import objects.FeatureList;
import objects.Form;
import objects.Lexicon;
import objects.LexiconList;
import objects.Stem;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/* Accessible only through LexiconModel */
class LexiconXMLReader {
	
	private Lexicon lexicon;
	
	public LexiconXMLReader() {}
	
	public void read(File file) throws JDOMException, IOException {
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(file);
		lexicon = getXML(doc.getRootElement());
	}
	
	private Lexicon getXML(Element root) {
		Lexicon lexicon = new Lexicon();
		
		if(root.getAttribute("label")!=null)
			lexicon.setLabel(root.getAttributeValue("label"));
		
		for( Element lexiconList: root.getChildren("lexiconList")) {
			
			LexiconList lList = new LexiconList();
			if(lexiconList.getAttributeValue("label")!=null)
				lList.setLabel(lexiconList.getAttributeValue("label"));
			
			if(lexiconList.getChildren("formList") != null)
				for(Element form : lexiconList.getChildren("formList"))
					if(form.getChildren("value") != null)
						for(Element children: form.getChildren())
							lList.addFormListItem(children.getText());
			//added form list to lexicon list
			
			if(lexiconList.getChildren("featureList") != null) {
				for(Element feature : lexiconList.getChildren("featureList")) {
					FeatureList fList = new FeatureList();

					if(feature.getAttributeValue("name") != null)
						fList.setName(feature.getAttributeValue("name"));
					
					if(feature.getChildren("value") != null)
						for(Element children: feature.getChildren())
							fList.addValuesItem(children.getText());
					lList.addFeatureListItem(fList);
				}
			}//added feature list to Lexicon list
			
			if(lexiconList.getChildren("stemList") != null) {
				for(Element stem : lexiconList.getChildren("stemList")) {
					Stem s = new Stem();

					if(stem.getAttributeValue("value") != null)
						s.setValue(stem.getAttributeValue("value"));
					
					if(stem.getAttributeValue("concept") != null)
						s.setConcept(stem.getAttributeValue("concept"));
					
					if(stem.getAttributeValue("sense") != null)
						s.setSense(stem.getAttributeValue("sense"));
					
					if(stem.getChildren("featureList") != null) {
						for(Element f: stem.getChildren("featureList")) {
							for(Element featureList: f.getChildren()) {
								Feature feature = new Feature();
								
								if(featureList.getAttributeValue("name") != null)
									feature.setName(featureList.getAttributeValue("name"));
								
								if(featureList.getAttribute("value") != null)
									feature.setValue(featureList.getAttributeValue("value"));;
									
								s.addFeatureListItem(feature);
							}
						}
					}
					
					if(stem.getChildren("formList") != null) {
						for(Element children: stem.getChildren("formList"))
							if(children.getChildren("form")!=null)
								for(Element form : children.getChildren("form")) {
									Form Form = new Form();
									if(form.getAttributeValue("name") != null)
										Form.setName(form.getAttributeValue("name"));
									
									if(form.getAttributeValue("value") != null)
										Form.setValue(form.getAttributeValue("value"));
									s.addFormListItem(Form);
								}
					}
					lList.addStemListItem(s);
				}
			}
			lexicon.addLexiconListItem(lList);
		}
		
		return lexicon;
	}
	
	public Lexicon getLexicon() {
		return lexicon;
	}
}
